/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.palette.swing;

/**
 *
 * @author Mr.Sam
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import fr.ove.palette.*;
import fr.ove.palette.swing.*;
import fr.ove.palette.events.*;

public class JPaletteEditor extends JTabbedPane implements PaletteListener {
    // We implement the PaletteListener because we want the JPaletteEditor to be
    // the source of the PaletteEvent generated following an action on a palette it
    // contains. So, The instance intercept the event generated by a palette it contains
    // and fires a PaletteEvent event with the same parameters, but with the instance
    // a source.
    
    /**
    * The list of listeners of the palette
    */
    private Vector listeners = new Vector();
    
    /**
    * The constructor.
    */
    public JPaletteEditor() {
        PaletteContainer paletteContainer = new FunctionsPalette();
        paletteContainer.addPaletteListener(this);
        addTab("Functions", paletteContainer);
        
        paletteContainer = new CalculusPalette();
        paletteContainer.addPaletteListener(this);
        addTab("Calculus", paletteContainer);
        
        paletteContainer = new OperatorPalette();
        paletteContainer.addPaletteListener(this);
        addTab("Operators", paletteContainer);
    }
    
    /**
    * Registers another listener to be informed of changes of the palette.
    * @param paletteListener a listener to add.
    */
    public void addPaletteListener(PaletteListener paletteListener) {
        listeners.addElement(paletteListener);
    }

    /**
    * Removes a listener.
    * @param paletteListener the listener to remove.
    */
    public void removePaletteListener(PaletteListener paletteListener) {
        listeners.removeElement(paletteListener);
    }
    
    /**
    * Fires a PaletteEvent event to registered listeners.
    * @param paletteEvent event encapsulating relevant information.
    */
    public void firePaletteEvent(PaletteEvent paletteEvent) {
        for (int i = 0; i < listeners.size(); i++)
          ((PaletteListener) listeners.elementAt(i)).consumePaletteEvent(paletteEvent);
    }
    
    /**
    * Consumes (i.e. treats) the event received.
    * @param paletteEvent the event to consume.
    */
    public void consumePaletteEvent(PaletteEvent paletteEvent) {
        PaletteEvent evt = new PaletteEvent(this);
        evt.setPaletteInfo(paletteEvent.getPaletteInfo());
        firePaletteEvent(evt);
    }
}